package com.example.hys.teamList;

import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;

import com.example.superyi.myteam.R;

import java.util.ArrayList;
import java.util.List;

/**
 * 步骤：
 *         1.获取RecyclerView对象 。
 *         2.初始化数据 。
 *         3.适配器实例化 。
 *         4.设置LayoutManager
 *         5. 设置Adapter 。
 */
public class MainActivity extends AppCompatActivity {

    private List<TeamBean> teamList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_teamlist);
        initTeam();
        RecyclerView recyclerView = findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        recyclerView.setLayoutManager(linearLayoutManager);
        TeamAdapter teamAdapter = new TeamAdapter(teamList);
        recyclerView.setAdapter(teamAdapter);
    }

    private void initTeam() {
        String sqlSelect = "select * from Team ";
        //扫描数据库，将信息放入teamList
        //打开数据库
        DatabaseHelper db = new DatabaseHelper(this,"TeamMessage.db",null,2);
        //获取数据库
        SQLiteDatabase sd = db.getReadableDatabase();
        Cursor cursor = sd.rawQuery(sqlSelect,new String[]{});
        while(cursor.moveToNext()){
            int team_id = cursor.getInt(cursor.getColumnIndex("team_id"));
            String  competition = cursor.getString(cursor.getColumnIndex("competition"));
            String  cpt_category = cursor.getString(cursor.getColumnIndex("cpt_category"));
            String  team_name = cursor.getString(cursor.getColumnIndex("team_name"));
            String  team_leader = cursor.getString(cursor.getColumnIndex("team_leader"));
            int mem_number = cursor.getInt(cursor.getColumnIndex("mem_number"));

            TeamBean teamBean = new TeamBean(team_id,competition,cpt_category,team_name,team_leader,mem_number);
            teamList.add(teamBean);
        }
        cursor.close();
    }
}
